from mcp.server.fastmcp import FastMCP
import math

# Create MCP server for math calculations
mcp = FastMCP("Math Calculator Server")

@mcp.tool()
def add(a: float, b: float) -> float:
    """Add two numbers"""
    return a + b

@mcp.tool()
def subtract(a: float, b: float) -> float:
    """Subtract two numbers"""
    return a - b

@mcp.tool()
def multiply(a: float, b: float) -> float:
    """Multiply two numbers"""
    return a * b

@mcp.tool()
def divide(a: float, b: float) -> str:
    """Divide two numbers with error handling"""
    try:
        if b == 0:
            return "Error: Cannot divide by zero"
        return a / b
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def power(base: float, exponent: float) -> float:
    """Calculate power of a number"""
    return base ** exponent

@mcp.tool()
def square_root(number: float) -> str:
    """Calculate square root with error handling"""
    try:
        if number < 0:
            return "Error: Cannot calculate square root of negative number"
        return math.sqrt(number)
    except Exception as e:
        return f"Error: {str(e)}"

if __name__ == "__main__":
    mcp.run(transport="stdio")